package haizhi.fruitmall.service.impl;

import com.github.pagehelper.PageHelper;
import haizhi.fruitmall.dao.mapper.GoodsCategoryMapper;
import haizhi.fruitmall.dao.mapper.GoodsMapper;
import haizhi.fruitmall.dao.mapper.GoodsUnitMapper;
import haizhi.fruitmall.exception.ResultException;
import haizhi.fruitmall.pojo.form.GoodsForm;
import haizhi.fruitmall.pojo.form.GoodsUnitForm;
import haizhi.fruitmall.pojo.po.*;
import haizhi.fruitmall.pojo.view.GoodsUnitView;
import haizhi.fruitmall.service.GoodsUnitService;
import haizhi.fruitmall.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Date: 2017/12/22 22:38
 * @Author: Richard Zane
 */

@Service
@Transactional
public class GoodsUnitServiceImpl implements GoodsUnitService{

    @Autowired
    private GoodsUnitMapper goodsUnitMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Override
    public void insert(GoodsUnitForm form) {
        GoodsUnit record = new GoodsUnit();
        BeanUtil.copyBean(form,record);
        record.setUnitId(BeanUtil.longId());
        goodsUnitMapper.insertSelective(record);
    }

    @Override
    public void update(GoodsUnitForm form) {
        GoodsUnit record = this.getone(form.getUnitId());
        if (BeanUtil.isNull(record)) { throw new ResultException("记录不存在！"); }
        BeanUtil.copyBean(form,record);
        record.setUpdateTime(BeanUtil.newTime());
        goodsUnitMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public void delete(Long id) {
        if (BeanUtil.isNull(this.getone(id))) { throw new ResultException("记录不存在！"); }
        goodsUnitMapper.deleteByPrimaryKey(id);
    }

    @Override
    public GoodsUnit getone(Long id) {
        if(BeanUtil.isNull(id)) throw new ResultException("编号为空！");
        return goodsUnitMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<GoodsUnit> getall(GoodsUnitForm form) {
        GoodsUnitExample example = new GoodsUnitExample();
        GoodsUnitExample.Criteria criteria = example.createCriteria();
        if(BeanUtil.noEmpty(form.getUnitName())){
            criteria.andUnitNameLike(BeanUtil.isLike(form.getUnitName()));
        }
        if(BeanUtil.noEmpty(form.getUnitIds())){
            criteria.andUnitIdIn(form.getUnitIds());
        }
        if(BeanUtil.noNull(form.getGoodsId())){
            criteria.andGoodsIdEqualTo(form.getGoodsId());
        }
        if(BeanUtil.noNull(form.getOrderBy())) {
            switch (form.getOrderBy()){
                case 1 : example.setOrderByClause("unit_price asc");break;
                case 2 : example.setOrderByClause("unit_price desc");break;
            }
        }
        if(BeanUtil.noNull(form.getPageNum()) && BeanUtil.noNull(form.getPageSize())){
            PageHelper.startPage(form.getPageNum(),form.getPageSize());
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        List<GoodsUnit> goodsUnitList = goodsUnitMapper.selectByExample(example);
        for(GoodsUnit goodsUnit : goodsUnitList){
            String picturePath = BeanUtil.getPath(request)+goodsUnit.getUnitPicture();
            goodsUnit.setUnitPicture(picturePath);
        }
        return goodsUnitList;
    }

    @Override
    public List<GoodsUnitView> getView(GoodsForm goodsForm){
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria goodsExampleCriteria = goodsExample.createCriteria();
        if(BeanUtil.noNull(goodsForm.getIsSpecial())){
            goodsExampleCriteria.andIsSpecialEqualTo(goodsForm.getIsSpecial());
        }
        if(BeanUtil.noNull(goodsForm.getCategoryId())){
            List<Integer> categoryIdList = new ArrayList<>();
            //父级编号
            categoryIdList.add(goodsForm.getCategoryId());
            //子级编号
            GoodsCategoryExample goodsCategoryExample = new GoodsCategoryExample();
            goodsCategoryExample.or().andCategoryPidEqualTo(goodsForm.getCategoryId());
            List<GoodsCategory> childCategoryList = goodsCategoryMapper.selectByExample(goodsCategoryExample);
            if(BeanUtil.noEmpty(childCategoryList)){
                for(GoodsCategory goodsCategory : childCategoryList){
                    categoryIdList.add(goodsCategory.getCategoryId());
                }
            }
            goodsExampleCriteria.andCategoryIdIn(categoryIdList);
        }
        if(BeanUtil.noNull(goodsForm.getOrderBy())){
            switch (goodsForm.getOrderBy()){
                case 3 : goodsExample.setOrderByClause("comment_times desc");break;
                case 4 : goodsExample.setOrderByClause("create_time desc");break;
                case 5: goodsExample.setOrderByClause("total_sales desc"); break;
            }
        }
        if(BeanUtil.noNull(goodsForm.getPageNum()) && BeanUtil.noNull(goodsForm.getPageSize())){
            PageHelper.startPage(goodsForm.getPageNum(),goodsForm.getPageSize());
        }
        List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);
        List<GoodsUnitView> goodsUnitViewList = new ArrayList<>();
        if(BeanUtil.noEmpty(goodsList)){
            List<Long> unitIdList = new ArrayList<>();
            for(Goods goods : goodsList){
                unitIdList.add(goods.getUnitId());
            }
            List<GoodsUnit> goodsUnitList = this.getall(new GoodsUnitForm(unitIdList));
            for(GoodsUnit goodsUnit : goodsUnitList){
                GoodsUnitView goodsUnitView = new GoodsUnitView();
                BeanUtil.copyBean(goodsUnit, goodsUnitView);
                goodsUnitViewList.add(goodsUnitView);
            }
        }
        return goodsUnitViewList;
    }
}
